package com.easylinkin.linkappapi.keywork.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.keywork.utils.ExcelExportUtils;
import com.easylinkin.linkappapi.keywork.dto.KeyworkListDto;
import com.easylinkin.linkappapi.keywork.entity.KeyworkList;
import com.easylinkin.linkappapi.keywork.service.KeyworkListService;
import com.easylinkin.linkappapi.keywork.vo.KeyworkListVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author TongJie
 * @since 2020-04-16
 */
@RestController
@RequestMapping("/keyworkList")
public class KeyworkListController {

    @Autowired
    private KeyworkListService service;

    @PostMapping("/getPage")
    @ApiOperation("查询分页")
    public RestMessage selectPage(@RequestBody RequestModel<KeyworkList> requestModel) {
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<KeyworkListVo> record = service.selectPage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder(record).build();
    }

    @PostMapping("/list")
    @ApiOperation("查询列表")
    public RestMessage selectList(@RequestBody KeyworkList keyworkList) {
        List<KeyworkListVo> record = service.selectList(keyworkList);
        return RestBuilders.successBuilder(record).build();
    }

    @PostMapping("/listTree")
    @ApiOperation("查询列表树")
    public RestMessage listTree(@RequestBody KeyworkList keyworkList) {
        List<KeyworkListVo> record = service.selectListTree(keyworkList);
        return RestBuilders.successBuilder(record).build();
    }

    @PostMapping("/updateList")
    @ApiOperation("设置清单")
    public RestMessage updateList(@RequestBody KeyworkListDto dto) {
        if (Objects.isNull(dto.getKeyworkList())){
            return RestBuilders.failureMessage().setMessage("新增参数不能为空");
        }
        return RestBuilders.successBuilder(service.updateKeyworkList(dto.getKeyworkList())).build();
    }

    @GetMapping("/exportList")
    public void exportList(HttpServletResponse response) throws IOException {

        List<Map<String, Object>> list = service.selectIsUsedListMap();
        // 1. 准备表头
        String[] headers = {"序号", "分类", "工作内容"};

        // 2. 准备数据
        List<Map<String, Object>> dataList = new ArrayList<>();

        int i = 1;
        for (Map<String, Object> map : list){
            if (StringUtils.isEmpty(map.get("parentContentName").toString())){
                continue;
            }
            Map<String, Object> insertMap = new HashMap<>();
            insertMap.put("序号",i++);
            insertMap.put("分类",map.get("parentContentName").toString());
            insertMap.put("工作内容",map.get("contentName") == null ?"":map.get("contentName").toString());
            dataList.add(insertMap);
        }


        // 3. 准备合并区域 [起始行, 结束行, 起始列, 结束列]
        List<int[]> mergeRegions = new ArrayList<>();
        mergeRegions.add(new int[]{1, 2, 1, 3}); // 合并第一行第1-3列

        // 4. 导出Excel
        ExcelExportUtils.exportWithTitle(response, "关键作业清单", "关键作业清单","关键作业清单", headers, dataList);
    }
}
